﻿@using Swimj.RazorLib.Components.Drawer
@inherits LayoutComponentBase
@inject ISecureCache SecureCache
@inject AuthenticationStateProvider AuthenticationStateProvider

<MudThemeProvider Theme="_theme"></MudThemeProvider>
<MudDialogProvider/>
<MudSnackbarProvider/>
<MudDrawerProvider Width="600px"/>
<AuthorizeView>
    <Authorized>
        <MudLayout>
            <MudAppBar Elevation="0" Class="app-layout-nav">
                @*<MudHidden Breakpoint="Breakpoint.SmAndDown">
                <MudIcon Icon="@Icons.Custom.Brands.MudBlazor" Size="Size.Large" />
                </MudHidden>
                <MudHidden Breakpoint="Breakpoint.Xs">
                <MudText Typo="Typo.h6" Class="ml-4">Admin Dashboard</MudText>
                </MudHidden>
                <MudHidden Breakpoint="Breakpoint.Xs" Invert="true">
                <MudText Typo="Typo.subtitle2">Admin Dashboard</MudText>
                </MudHidden>*@
                <MudToolBar DisableGutters="true">
                    <MudIconButton Icon="@Icons.Material.Outlined.Menu" Color="Color.Inherit" OnClick="@(_ => DrawerToggle())" Class="ml-3"/>
                </MudToolBar>
                <MudSpacer/>
                <MudHidden Breakpoint="Breakpoint.MdAndUp" Invert="true">
                    <MudTooltip Text=" Switch Language">
                        <MudIconButton Icon="@Icons.Material.Filled.Translate" Color="Color.Inherit"/>
                    </MudTooltip>
                    @*<MudTooltip Text=" Toggle Dark Mode">
                    <MudIconButton Icon="@Icons.Material.Filled.Brightness4" Color="Color.Inherit" OnClick="@(_ => OpenThemeManager(true))"/>
                    </MudTooltip>*@
                    <MudTooltip Text="Fork Repository">
                        <MudIconButton Icon="@Icons.Custom.Brands.GitHub" Color="Color.Inherit" Target="_blank"/>
                    </MudTooltip>
                </MudHidden>
                <MudHidden Breakpoint="Breakpoint.SmAndDown" Invert="true">
                    <MudMenu Icon="@Icons.Material.Filled.Settings" Color="Color.Inherit" Dense="true">
                        <div class="px-2">
                            <MudIconButton Icon="@Icons.Material.Filled.Brightness4" Color="Color.Inherit"/>
                            <MudIconButton Icon="@Icons.Custom.Brands.GitHub" Color="Color.Inherit" Href="https://github.com/blazorhero/CleanArchitecture" Target="_blank"/>
                        </div>
                        <MudText Typo="Typo.body2" Class="px-4 py-2">
                            <b>"Community"</b>
                        </MudText>
                        <MudMenuItem Href="https://www.facebook.com/codewithmukesh" Target="_blank">"Facebook Page"</MudMenuItem>
                        <MudMenuItem Href="https://www.linkedin.com/in/iammukeshm/" Target="_blank">LinkedIn</MudMenuItem>
                        <MudText Typo="Typo.body2" Class="px-4 py-2">
                            <b>"Resources"</b>
                        </MudText>
                        <MudMenuItem Href="https://mudblazor.com/getting-started/installation" Target="_blank">"MudBlazor Documentation"</MudMenuItem>
                        <MudMenuItem Href="https://codewithmukesh.com/blog/blazor-hero-quick-start-guide/" Target="_blank">"Quick-Start Guide"</MudMenuItem>
                    </MudMenu>
                </MudHidden>
                <MudMenu Dense="true" Class="mt-1 ml-4"
                         AnchorOrigin="Origin.BottomRight" TransformOrigin="Origin.TopRight">
                    <ActivatorContent>
                        @CreateAvatar(context)
                        <MudText Style="height: 50px; line-height: 50px; margin-left: 6px;">@context?.User?.Identity?.Name</MudText>
                    </ActivatorContent>
                    <ChildContent>
                        <MudDivider Class="mb-2"/>
                        <MudListItem Text="个人信息" Href="/account" Icon="@Icons.Material.Outlined.Person"/>
                        <MudListItem Text="退出登录" Icon="@Icons.Material.Filled.Logout" OnClick="Logout"/>
                    </ChildContent>
                </MudMenu>
            </MudAppBar>
            <MudDrawer @bind-Open="@_drawerOpen" ClipMode="DrawerClipMode.Never"
                       Elevation="0" Class="app-layout-menu">
                <NavMenu></NavMenu>
            </MudDrawer>
            <MudMainContent>

                <MudBreadcrumbs Items="_items" Separator=">" Class="pl-5 pr-5"></MudBreadcrumbs>

                <MudContainer MaxWidth="MaxWidth.False" Class="mt-4">
                    <CascadingValue Value="this">
                        @Body
                    </CascadingValue>
                </MudContainer>
            </MudMainContent>

            @*<MudThemeManager Open="_themeManagerOpen" OpenChanged="OpenThemeManager" Theme="_themeManager"
            ThemeChanged="UpdateTheme"/>
            <MudThemeManagerButton OnClick="@(_ => _themeManagerOpen = true)"/>*@
        </MudLayout>
    </Authorized>
    <NotAuthorized>
        @Body
    </NotAuthorized>
</AuthorizeView>

@code {

    // private ThemeManagerTheme _themeManager = new ThemeManagerTheme
    // {
    //     DrawerClipMode = DrawerClipMode.Never
    // };

    private MudTheme _theme = new()
    {
        LayoutProperties = new LayoutProperties()
        {
            DrawerWidthLeft = "260px"
        }
    };

    // private bool _themeManagerOpen;
    private bool _drawerOpen = true;

    private List<BreadcrumbItem> _items = new()
    {
        new("Home", href: "#"),
        new("Link 1", href: "#"),
        new("Link 2", href: null, disabled: true)
    };

    protected override void OnInitialized()
    {
        StateHasChanged();
    }

    void DrawerToggle()
    {
        _drawerOpen = !_drawerOpen;
    }

    // void OpenThemeManager(bool value)
    // {
    //     _themeManagerOpen = value;
    // }

    // void UpdateTheme(ThemeManagerTheme value)
    // {
    //     _themeManager = value;
    //     StateHasChanged();
    // }

    async Task Logout()
    {
        await SecureCache.RemoveAsync("authToken");
        await SecureCache.RemoveAsync("refreshToken");
        ((CustomAuthStateProvider)AuthenticationStateProvider).MarkUserAsLoggedOut();
    }

    RenderFragment CreateAvatar(AuthenticationState authenticationState)
    {
        var avatar = authenticationState?.User?.FindFirst("avatar");
        return
            @<MudAvatar Style="height: 50px; width: 50px;">

                @if (avatar != null)
                {
                    <MudImage Src="@avatar.Value.ToString()"></MudImage>
                }
                else
                {
                    <MudIcon Icon="mdi mdi-account"></MudIcon>
                }
            </MudAvatar>;
    }

}